package completeFuture.domain;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

public class Shop {

    private String shopName;

    public Shop(String shopName) {
        this.shopName = shopName;
    }

    public String getShopName() {
        return shopName;
    }

    public static void delay() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getPrice(String product) {
        delay();
        double price = new Random().nextDouble();
        return String.format("shop:%s,product:%s,price:%f",
                shopName,
                product,
                price);
    }

    public Future<String> getPriceAsyn(String product) {
        CompletableFuture<String> priceFuture = new CompletableFuture<>();
        new Thread(() -> {
            try {
                String price = getPrice(product);
                priceFuture.complete(price);
            } catch (Exception e) {
                priceFuture.completeExceptionally(e);
            }
        }).start();
        return priceFuture;
    }
}
